<script setup>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
</script>

<template>
<canvas id="myCanvas" width="360" height="650"></canvas>
</template>

<script>
import facecat from './facecat.js';
let gPaint = new facecat.FCPaint();

/*
* 点击方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
* clicks:点击次数
*/
let onClick = function (view, firstTouch, firstPoint, secondTouch, secondPoint, clicks) {
    facecat.onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks);
};

/*
* 鼠标按下实现方法
* view:视图
* mp:坐标
* button:按钮
* clicks:点击次数
* delta:滚轮值
*/
let onMouseDown = function (view, mp, buttons, clicks, delta) {
	facecat.onMouseDownDefault(view, mp, buttons, clicks, delta);
};

/*
* 鼠标移动实现方法
* view:视图
* mp:坐标
* button:按钮
* clicks:点击次数
* delta:滚轮值
*/
let onMouseMove = function (view, mp, buttons, clicks, delta) {
	facecat.onMouseMoveDefault(view, mp, buttons, clicks, delta);
};

/*
* 鼠标滚动实现方法
* view:视图
* mp:坐标
* button:按钮
* clicks:点击次数
* delta:滚轮值
*/
let onMouseWheel = function (view, mp, buttons, clicks, delta) {
	if (window.event.ctrlKey && view.paint) {
		//放大显示
		if (delta < 0) {
			view.paint.scaleFactorX = view.paint.scaleFactorX + 0.1;
			view.paint.scaleFactorY = view.paint.scaleFactorY + 0.1;
			//resizeAll();
			//缩小显示
		} else if (delta > 0) {
			if (view.paint.scaleFactorX > 0.2) {
				view.paint.scaleFactorX = view.paint.scaleFactorX - 0.1;
				view.paint.scaleFactorY = view.paint.scaleFactorY - 0.1;
				//resizeAll();
			}
		}
		return;
	}
	facecat.onMouseWheelDefault(view, mp, buttons, clicks, delta);	
};

/*
* 鼠标抬起实现方法
* view:视图
* mp:坐标
* button:按钮
* clicks:点击次数
* delta:滚轮值
*/
let onMouseUp = function (view, mp, buttons, clicks, delta) {
	facecat.onMouseUpDefault(view, mp, buttons, clicks, delta);	
};

/*
* 触摸开始方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchBegin = function (view, firstTouch,  firstPoint, secondTouch, secondPoint) {
	facecat.onTouchBeginDefault(view, firstTouch,  firstPoint, secondTouch, secondPoint);
};

/*
* 触摸移动方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchMove = function (view, firstTouch,  firstPoint, secondTouch, secondPoint) {
	facecat.onTouchMoveDefault(view, firstTouch,  firstPoint, secondTouch, secondPoint);	
};

/*
* 触摸结束方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
let onTouchEnd = function (view, firstTouch,  firstPoint, secondTouch, secondPoint) {
	facecat.onTouchEndDefault(view, firstTouch,  firstPoint, secondTouch, secondPoint);	
};

/*
 * 重置大小
 */
let resizeAll = function () {
	try {
		const canvas = document.getElementById('myCanvas');
		canvas.width = document.documentElement.clientWidth;
		canvas.height = document.documentElement.clientHeight;
		facecat.scaleCanvas(gPaint);
		facecat.updateViewDefault(gPaint.views);
	} catch (err) {
	}
	facecat.invalidate(gPaint);
};
	
export default { 
	data() {
		return {
		}
	},
	onLoad() {
	}, 
	mounted(){
			const canvas = document.getElementById('myCanvas');
			gPaint.isMobile = facecat.isMobileMode();
			window.addEventListener('resize', resizeAll, true);
			canvas.width = window.innerWidth;
			canvas.height = window.innerHeight;
			gPaint.canvas = canvas;
			facecat.scaleCanvas(gPaint);
			//注册事件
			facecat.addMouseDownEvent(canvas, onMouseDown);
			facecat.addMouseMoveEvent(canvas, onMouseMove);
			facecat.addMouseWheelEvent(canvas, onMouseWheel);
			facecat.addMouseUpEvent(canvas, onMouseUp, onClick);
			facecat.addTouchBeginEvent(canvas, onTouchBegin);
			facecat.addTouchMoveEvent(canvas, onTouchMove);
			facecat.addTouchEndEvent(canvas, onTouchEnd, onClick);
			/*
			* 重绘背景的实现方法
			* view:视图
			* paint:绘图对象
			* clipRect:裁剪区域
			*/
			gPaint.onPaint = function (view, paint, clipRect) {
				facecat.onPaintDefault(view, paint, clipRect);
			};
					
			/*
			* 重绘边框的实现方法
			* view:视图
			* paint:绘图对象
			* clipRect:裁剪区域
			*/
			gPaint.onPaintBorder = function (view, paint, clipRect) {
				facecat.onPaintBorderDefault(view, paint, clipRect);
			};
			 /*
			  * 更新布局的实现方法
			  * views:视图集合
			  */
			gPaint.onUpdateView = function (views) {
				facecat.updateViewDefault(views);
			}
			//解析加载XML
			let xml = `<?xml version="1.0" encoding="utf-8"?>
			<html xmlns="facecat">
			  <head>
			  </head>
			  <body>
				<div dock="fill" type="tab" selectedindex="0">
				  <div name="TabPage" text="可视化生成" type="tabpage">
					<calendar name="Calendar" location="35,50" size="250,300" />
				  </div>
				  <div name="TabPage2" text="代码创建" type="tabpage" />
				</div>
			  </body>
			</html>
			`; 
			facecat.renderFaceCat(gPaint, xml);
			//获取第二页
			let tabPage = facecat.findViewByName("TabPage2", gPaint.views);
			let calendar = new facecat.FCCalendar();
			calendar.location = new facecat.FCPoint(100, 100);
			calendar.size = new facecat.FCSize(200, 300);
			facecat.addViewToParent(calendar, tabPage);
			facecat.initCalendar(calendar);
			calendar.selectedDay = facecat.getYear(calendar.years, 2022).months.get(10).days.get(1);
			facecat.updateCalendar(calendar);
			},
			methods: {  
	}
}
</script>
